import scrapy
import logging
from scrapy.linkextractors import LinkExtractor
from scrapy.spiders import CrawlSpider, Rule

logger = logging.getLogger("BqgSpider")


class BqgSpider(CrawlSpider):
    name = 'bqg'
    allowed_domains = ['xbiquge.la']
    start_urls = ['http://www.xbiquge.la/paihangbang/']

    rules = (
        Rule(LinkExtractor(restrict_xpaths="//div[@class='box b1'][1]/ul/li/a"),
             callback='parse_chapters', follow=True),
        Rule(LinkExtractor(restrict_xpaths="//div[@id='list']/dl/dd/a"),
             callback='parse_content')
    )

    def parse_chapters(self, response):
        """解析小说--专题"""
        item = {}
        title = response.xpath("//div[@id='info']/h1").extract_first()
        logger.info("----------------->" + title)
        return item

    def parse_content(self, response):
        """解析章节--内容"""
        item = {}
        name = response.xpath("//div[@class='bookname']/h1").extract_first()
        novel = response.xpath("//div[@class='con_top']/a[3]").extract_first()
        logger.info("==================" + name + "|" + novel)
        return item
